// 
//  grabber.js
//  firefox
//  
//  Created by Zak on 2008-07-14.
//  Copyright 2008 Ant.com. All rights reserved.
// 

top.AntGrabber =  {
	
	queryObserver: null,
	htmlParser: null,
	state: false,
	isStarted: function () { return this.state; },
	
    /**
     * This method starts observing firefox requests and set a onload event
	 * to be able to parse the current page and detect flvs
     */
    init: function () 
    {
		this.queryObserver = new AntQueryObserver();
		this.htmlParser = new AntHtmlParser();
		this.start();
    },
    
    /**
     * Initialize the HTML Parser class
     */
    initHtmlParser: function (event)
    {
        var that = this;
        
        return function (event) {
            if (event.originalTarget instanceof HTMLDocument)
            {
                var currentDoc = event.originalTarget;
                var rootDoc = currentDoc;

                if (event.originalTarget.defaultView.frameElement)
                {
                    while (rootDoc.defaultView.frameElement)
                        rootDoc = rootDoc.defaultView.frameElement.ownerDocument;
                    // AntLib.toLog('Frame: ' + currentDoc.location.href);
                }
                else
                {
                    // AntLib.toLog('RootPage: ' + currentDoc.location.href);
                }
                that.htmlParser.onShowPage(currentDoc, rootDoc);
            }
        }
    },
    
    /**
     * Start the grabber
     */
    start: function ()
    {
        window.addEventListener("load", function () {gBrowser.addEventListener("DOMContentLoaded", AntGrabber.initHtmlParser(), true);}, false);
    	observerService.addObserver(this.queryObserver, "http-on-examine-response", false);
		this.state = true;
		AntFlvUi.setContent("list");
    },
    
	/**
	 * Called when a new flv is found
	 * @param flvLink       The found flv object
	 */
	foundFlvLink: function (flvLink)
	{
        AntLib.toLog("AntGrabber: New video found [Origin: "+flvLink.origin+", URL: "+flvLink.url+", Name: "+flvLink.name+"]");
		AntFlvList.push(flvLink);
		flvLink.doc.__antflv__ = flvLink;
		AntFlvUi.updateDownloadButton();
	}
}
